#pragma once

#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <jsoncpp/json/json.h>
const std::string sep = "\r\n";
bool Encode(std::string& message)
{
    if (message.empty()) return false;
    message = std::to_string(message.size()) + 
    sep + message + sep;

    return true;
}
bool Decode(std::string& package, std::string* content)
{
    auto pos = package.find(sep);
    if (pos == std::string::npos) return false;
    std::string content_length_str = package.substr(0, pos);
    int content_length = std::stoi(content_length_str);
    int full_length = content_length_str.size() + 
    2 * sep.size() + content_length;
    // ! 重要的是我们需要确认报文的完整性
    if (package.size() >= full_length) 
    {
        *content = package.substr(pos + sep.size(), content_length);
        package.erase(0, full_length); // * 写网络代码的时候再进行研究的
        return true;
    }
    else return false;

}
class Request
{
private:
    double _x, _y;
    char _oper;

public:
    Request(double x, double y, char oper) : _x(x),
                                             _y(y),
                                             _oper(oper)
    {
    }
    Request ():_x(0), _y(0), _oper('+') {}
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root, &ss);
        out_string = ss.str();
        return true;
    }
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            std::cout << "Failed to parse Json: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }
        _x = root["x"].asDouble();
        _y = root["y"].asDouble();
        _oper = root["oper"].asInt();
        return true;
    }
    void print()
    {
        std::cout << _x << std::endl;
        std::cout << _oper << std::endl;
        std::cout << _y << std::endl;
    }
    double X() const {return _x;}
    double Y() const {return _y;}
    char Oper() const {return _oper;}
    
};

class Response
{
private:
    double _ret; //* 结果
    int _code;   //* 错误码
public:
    Response() :_ret(0), _code(0){

    }
    void setResult(double&& res) {_ret = res;}
    void setCode(int code) {_code = code;}
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["ret"] = _ret;
        root["code"] = _code;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root, &ss);
        out_string = ss.str();
        return true;
    }
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            std::cout << "Failed to parse Json: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }
        _ret = root["ret"].asDouble();
        _code = root["code"].asInt();
        return true;
    }
    double Ret() {return _ret;}
    int Code() {return _code;}
};